package com.room.controller.dorm;

import com.room.pojo.*;
import com.room.service.*;
import com.room.utils.UtilFuns;
import javafx.beans.binding.ObjectExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建人 lin
 * @创建时间 2019/10/7
 * @描述：分配和腾空宿舍的
 */
@RestController
@RequestMapping("/disandpu/manager")
public class DisAndPuController {
    @Autowired
    StudentService studentService;

    @Autowired
    CollegeService collegeService;

    @Autowired
    ClassesService classesService;

    @Autowired
    ApartmentService apartmentService;

    @Autowired
    BedroomService bedroomService;

    /**
     * 根据专业分配宿舍
     * @param disBy
     * @return
     */
    @RequestMapping(value = "/disbyCollege",method = RequestMethod.POST)
    public Map<String,Object> disByCollege(DisBy disBy){
        //返回的结果数据
        Map<String,Object> resultMap = new HashMap<String,Object>();
        System.out.println(disBy.toString());
        String classId = disBy.getClassId();
//        String sex1 = disBy.getSex1();
        String apartmentId = disBy.getApartmentId();
        String apartmentId2 = disBy.getApartmentId2();
//        String sex2 = disBy.getSex2();
        String grade = disBy.getGrade();
        Map<String,String> map = new HashMap<String,String>();
        if (UtilFuns.isNotEmpty(classId)){
            map.put("classId",classId);
        }else {classId = null;}
//        if(UtilFuns.isNotEmpty(sex1)) {
//            if(sex1.equals("b")) {map.put("sex1", "男");}
//            else if(sex1.equals("g")) {map.put("sex1", "女");}
//        } else {sex1=null;}
//        if(UtilFuns.isNotEmpty(sex2)) {
//            if(sex2.equals("b")){ map.put("sex2", "男");}
//            else if(sex2.equals("g")) {map.put("sex2", "女");}
//        } else {sex2=null;}
        if(UtilFuns.isNotEmpty(apartmentId)) {  map.put("apartmentId", apartmentId); } else {apartmentId=null;}
        if(UtilFuns.isNotEmpty(apartmentId2)) {  map.put("apartmentId2", apartmentId2); } else {apartmentId2=null;}
        if(UtilFuns.isNotEmpty(grade)){map.put("grade",grade);}else{grade=null;}
//
//        String info="<br/>宿舍分配情况<br/><br/>";
        int forBrIndex=-1;
        int studentIndex=0;

//        if(sex1!=null && apartmentId!=null){}
//        if(sex2!=null && apartmentId2!=null){}

        Map paraMap=new HashMap();
        paraMap.put("collegeId",classId);
        paraMap.put("status","N");
        paraMap.put("sex","男");
        paraMap.put("grade",grade);
        List<Student> boyStudentList=studentService.find(paraMap);
        int studentNum=boyStudentList.size();
        System.out.println("studentNmum="+studentNum);
        int forBrNum=( (studentNum*1.0/5-studentNum/5)<0.0001?studentNum/5:studentNum/5+1);
        Map paraMap1=new HashMap();
        paraMap1.put("status","N");
        paraMap1.put("apmId",apartmentId);
        List<Bedroom> bedroomList1= bedroomService.find(paraMap1);
        //男生宿舍是否满员提示信息
        String mannotice = "";
        //返回成功分配宿舍的男学生信息
        List<DisedStudent> manlist = new ArrayList<>();
        for(Student student:boyStudentList){
            studentIndex++;
            if(studentIndex%5==1) {forBrIndex++;}
            Bedroom br;
            if(forBrIndex==bedroomList1.size()-1  ) {
                mannotice+="你选择分配的男生公寓已满员，请稍后再未未分配的男生分配公寓";
                break;
            }else if (bedroomList1.size()==0){
                mannotice+="你选择分配的男生公寓已满员，请稍后再未未分配的男生分配公寓";
                break;
            }
            else{
                br=bedroomList1.get(forBrIndex);
            }
            student.setBedroomId(br.getBedroomId());
            student.setBedroomName(br.getBedroomName());
            student.setStatus("Y");
            studentService.update(student);
            br.setStatus("Y");
            br.setTotalBed( (studentIndex%5==0?5:studentIndex%5)+"/5");
            bedroomService.update(br);
            //返回分配宿舍成功的学生信息
            DisedStudent disedStudent  = new DisedStudent();
            disedStudent.setClassName(student.getClassName());
            disedStudent.setBedroomName(student.getBedroomName());
            disedStudent.setStudentName(student.getStudentName());
            manlist.add(disedStudent);
//            info+=student.getClassName()+" : "+student.getStudentName()+"   分配的宿舍    "+br.getBedroomName()+"<br/>";
    }
//        info+="<br/> 女生：<br/>";

        forBrIndex=-1;
        studentIndex=0;

        Map paraMap11=new HashMap();
        paraMap11.put("collegeId",classId);
        paraMap11.put("status","N");
        paraMap11.put("sex","女");
        paraMap11.put("grade",grade);
        List<Student> girlStudentList=studentService.find(paraMap11);
        int studentNum2=girlStudentList.size();
        int forBrNum2=( (studentNum2*1.0/5-studentNum2/5)<0.0001?studentNum2/5:studentNum2/5+1);
        System.out.println("该学院未分配宿舍的女生人数:"+studentNum2);
        System.out.println("至少需要宿舍间数:"+forBrNum2);

        Map paraMap2=new HashMap();
        paraMap2.put("status","N");
        paraMap2.put("apmId",apartmentId2);
        List<Bedroom> bedroomList2= bedroomService.find(paraMap2);
//        返回成功分配宿舍的女生信息
        List<DisedStudent> girllist = new ArrayList<>();
        //女生未分配宿舍提示
        String girlnotice = "";
        for(Student student:girlStudentList){
            studentIndex++;
            if(studentIndex%5==1){ forBrIndex++;}
            Bedroom br;
            if(forBrIndex==bedroomList2.size()-1  || bedroomList2.size()==0  ) {
                girlnotice+="你选择分配的女生公寓已满员，请稍后再未未分配的女生分配公寓";
                break;
            }else{
                br=bedroomList2.get(forBrIndex);
            }
            student.setBedroomId(br.getBedroomId());
            student.setBedroomName(br.getBedroomName());
            student.setStatus("Y");
            studentService.update(student);
            br.setStatus("Y");
            br.setTotalBed((studentIndex%5==0?5:studentIndex%5)+"/5");
            bedroomService.update(br);
            DisedStudent girldisedStudent = new DisedStudent();
            girldisedStudent.setStudentName(student.getStudentName());
            girldisedStudent.setBedroomName(student.getBedroomName());
            girldisedStudent.setClassName(student.getClassName());
//            info+=student.getClassName()+" : "+student.getStudentName()+"   分配的宿舍     "+br.getBedroomName()+"<br/>";
            girllist.add(girldisedStudent);
        }
//      Map<String,String> returnmap = new HashMap<String,String>();
//        info+="<br/>以上同学成功分配到宿舍<br/><br/>";
//        returnmap.put("return",info);
        resultMap.put("mannotice",mannotice);
        resultMap.put("girlnotice",girlnotice);
        resultMap.put("manlist",manlist);
        resultMap.put("girllist",girllist);
        return resultMap;
}

    /**
     * 根据班级分配宿舍
     * @param disBy
     * @return
     */
    @RequestMapping(value = "/disbyClass",method = RequestMethod.POST)
        public Map<String, Object> disByClass(DisByCls disBy){
        String collegeId = disBy.getCollegeId();
        String classId = disBy.getClassId();
        String sex1 = disBy.getSex1();
        String apartmentId = disBy.getApartmentId();
        String apartmentId2 = disBy.getApartmentId2();
        String sex2 = disBy.getSex2();

        String grade = disBy.getGrade();

        Map<String, String> map= new HashMap<String,String>();
        if(UtilFuns.isNotEmpty(collegeId)){map.put("collegeId",collegeId);}else collegeId=null;
        if(UtilFuns.isNotEmpty(classId)) {  map.put("classId", classId); } else classId=null;
        if(UtilFuns.isNotEmpty(sex1)) {
            if(sex1.equals("b")){ map.put("sex1", "男");}
            else if(sex1.equals("g")){ map.put("sex1", "女");}
        } else {sex1=null;}
        if(UtilFuns.isNotEmpty(sex2)) {
            if(sex2.equals("b")) {map.put("sex2", "男");}
            else if(sex2.equals("g")) {map.put("sex2", "女");}
        } else {sex2=null;}

        if(UtilFuns.isNotEmpty(apartmentId)) {  map.put("apartmentId", apartmentId); } else{ apartmentId=null;}
        if(UtilFuns.isNotEmpty(apartmentId2)) {  map.put("apartmentId2", apartmentId2); } else {apartmentId2=null;}
        int forBrIndex=-1;
        int studentIndex=0;

        if(sex1!=null && apartmentId!=null){}
        if(sex2!=null && apartmentId2!=null){}

        Map paraMap=new HashMap();
        paraMap.put("collegeId",collegeId);
        paraMap.put("classId",classId);
        paraMap.put("status","N");
        paraMap.put("sex","男");
        paraMap.put("grade",grade);
        List<Student> boyStudentList=studentService.find(paraMap);
        int studentNum=boyStudentList.size();
        int forBrNum=( (studentNum*1.0/5-studentNum/5)<0.0001?studentNum/5:studentNum/5+1);
        System.out.println("该班级未分配宿舍的男生人数:"+studentNum);
        System.out.println("至少需要宿舍间数:"+forBrNum);


        Map paraMap1=new HashMap();
        paraMap1.put("status","N");
        paraMap1.put("apmId",apartmentId);
        List<Bedroom> bedroomList1= bedroomService.find(paraMap1);
        //返回男生宿舍是否已分配满
       String mannotice = "";

       List<DisedStudent> manlist = new ArrayList<DisedStudent>();
        for(Student student:boyStudentList){
            studentIndex++;
            if(studentIndex%5==1){ forBrIndex++;}
            Bedroom br;
            if(forBrIndex==studentNum-1) {
                mannotice+="由于该公寓宿舍已分配满，该班级部分男同学未分配到宿舍 ，稍后请再次为他们分配宿舍!";
//				apartmentService.get(apartmentId).set
                break;
            }else{
                br=bedroomList1.get(forBrIndex);
            }
            student.setBedroomId(br.getBedroomId());
            student.setBedroomName(br.getBedroomName());
            student.setStatus("Y");
            studentService.update(student);
            br.setStatus("Y");
            br.setTotalBed( (studentIndex%5==0?5:studentIndex%5)+"/5");
            bedroomService.update(br);
            DisedStudent student1 = new DisedStudent();
            student1.setClassName(student.getClassName());
            student1.setStudentName(student.getStudentName());
            student1.setBedroomName(br.getBedroomName());
            manlist.add(student1);
        }
        forBrIndex=-1;
        studentIndex=0;

        Map paraMap11=new HashMap();
        paraMap.put("collegeId",collegeId);
        paraMap11.put("classId",classId);
        paraMap11.put("status","N");
        paraMap11.put("sex","女");
        List<Student> girlStudentList=studentService.find(paraMap11);
        int studentNum2=girlStudentList.size();
        int forBrNum2=( (studentNum2*1.0/5-studentNum2/5)<0.0001?studentNum2/5:studentNum2/5+1);
        System.out.println("该班级未分配宿舍的女生人数:"+studentNum2);
        System.out.println("至少需要宿舍间数:"+forBrNum2);

        Map paraMap2=new HashMap();
        paraMap2.put("status","N");
        paraMap2.put("apmId",apartmentId2);
        List<Bedroom> bedroomList2= bedroomService.find(paraMap2);
        String girlnotice = "";
        List<DisedStudent> girllist = new ArrayList<DisedStudent>();

        for(Student student:girlStudentList){
            studentIndex++;
            if(studentIndex%5==1){ forBrIndex++;}
            Bedroom br;
            if(forBrIndex==studentNum2-1) {
                girlnotice+="由于该公寓宿舍已分配满，该学院部分女同学未分配到宿舍 ，稍后请再次为他们分配宿舍!<br/>";
                break;
            }else{
                br=bedroomList2.get(forBrIndex);
            }
            student.setBedroomId(br.getBedroomId());
            student.setBedroomName(br.getBedroomName());
            student.setStatus("Y");
            studentService.update(student);
            br.setStatus("Y");
            br.setTotalBed((studentIndex%5==0?5:studentIndex%5)+"/5");
            bedroomService.update(br);
            DisedStudent student2 = new DisedStudent();
            student2.setClassName(student.getClassName());
            student2.setStudentName(student.getStudentName());
            student2.setBedroomName(br.getBedroomName());
            girllist.add(student2);
        }
      Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("mannotice",mannotice);
        resultMap.put("manlist",manlist);
        resultMap.put("girlnotice",girlnotice);
        resultMap.put("girllist",girllist);
        return resultMap;
        }

    /**
     * 根据专业清空宿舍
     * @param collegeId
     * @return
     */
    @RequestMapping(value = "/emptyByCollege",method = RequestMethod.POST)
        public Map<String, Object> emptybycollege(DisBy dis){
//                String info = "宿舍腾空情况:<br>";
        //返回的结果数据
        //这里获取的就是我的专业id
        String collegeId = dis.getClassId();
        String grade = dis.getGrade();
        Map<String,Object> resultMap = new HashMap<>();
                Map<String,String> map = new HashMap<String,String>();
                if(UtilFuns.isNotEmpty(collegeId)){
                    map.put("collegeId",collegeId);
                }else {
                    collegeId = null;
                }
        if(UtilFuns.isNotEmpty(grade)){
            map.put("grade",grade);
        }else {
           grade = null;
        }
                map.put("status","Y");
                //返回提示信息
                String notice = "";
                List<Student> studentList = studentService.find(map);
                if(studentList.size()<1){
//                    info+="该学院所有学生都没分配宿舍</br>";
//                    return info;
                    notice = "该学院还未分配宿舍";
                    resultMap.put("notice",notice);
                }
                String info="";
//                List<DisedStudent> disedStudents = new ArrayList<>();
        long count = 0;
                for(Student student:studentList){
//                   DisedStudent disedStudent  = new DisedStudent();
//                   disedStudent.setStudentName(student.getStudentName());
//                   disedStudent.setClassName(student.getClassName());
//                   disedStudent.setBedroomName(student.getBedroomName());
//                   disedStudents.add(disedStudent);
//                    info+="<"+student.getStudentName()+" "+student.getClassName()+" "+
//                    student.getCollegeName()+">该学生被腾出"+student.getBedroomName()+"宿舍<br>";
                    Bedroom bedroom = bedroomService.getBedRoomById(student.getBedroomId());
                    bedroom.setStatus("N");
                    bedroom.setTotalBed("0/5");
                    bedroomService.update(bedroom);

                    student.setStatus("N");
                    student.setBedroomId("");
                    student.setBedroomName("");
                    studentService.update(student);
                    count++;
                }
                String collegeName = collegeService.getCollegeNameByCollegeId(collegeId);
                String list = "清空"+collegeName+"共计"+String.valueOf(count)+"的床位";
                resultMap.put("notice",notice);
                //清空哪个专业多少人的宿舍
                resultMap.put("list",list);
                return resultMap;
        }

    /**
     * 按班级腾空宿舍
     * @param disBycls
     * @return
     */

    @RequestMapping(value = "/emptyByClass",method = RequestMethod.POST)
        public Map<String,Object> emptyclas(DisByCls disByCls){
        String collegeId = disByCls.getCollegeId();
        String classId = disByCls.getClassId();
        String grade = disByCls.getGrade();
            Map<String, String> map= new HashMap<String,String>();
            if(UtilFuns.isNotEmpty(collegeId)) {  map.put("collegeId", collegeId); } else collegeId=null;
            if(UtilFuns.isNotEmpty(classId)) {  map.put("classId", classId); } else classId=null;
        if(UtilFuns.isNotEmpty(grade)) {  map.put("grade", grade); } else grade=null;
            map.put("status", "Y");
            List<Student> studentList= studentService.find(map);
            String notice = "";
            if(studentList.size()<1){
                notice+="该班级所有学生都没分配到宿舍";
            }
           List<DisedStudent> disedStudents = new ArrayList<DisedStudent>();
            for(Student student:studentList){
                DisedStudent student3 = new DisedStudent();
                student3.setStudentName(student.getStudentName());
                student3.setClassName(student.getClassName());
                student3.setBedroomName(student.getBedroomName());
                disedStudents.add(student3);
                Bedroom bedroom=bedroomService.getBedRoomById(student.getBedroomId());
                bedroom.setStatus("N");
                bedroom.setTotalBed("0/5");
                bedroomService.update(bedroom);

                student.setStatus("N");
                student.setBedroomId("");
                student.setBedroomName("");
                studentService.update(student);
            }

            Map<String,Object> resultmap = new HashMap<String,Object>();
            resultmap.put("notice",notice);
            resultmap.put("class",disedStudents);
            return resultmap;
        }

    /**
     * 获取公寓下的房间
     * @param apartmentId
     * @return
     */
    @RequestMapping(value = "/getbedroom",method=RequestMethod.GET)
        public List<Bedroom> getBedroomNobyApartId(String apartmentId){
            Map paraMap1=new HashMap();
            paraMap1.put("apmId",apartmentId);
            List<Bedroom> bedroomList= bedroomService.find(paraMap1);
            return bedroomList;
        }

    /**
     * 根据个人分配宿舍
     * @param disBy
     * @return
     */
    @RequestMapping(value = "/disbyStu",method = RequestMethod.POST)
    public Map<String,Object> dispByStu(DisByStu disByStu){
        Map<String,Object> resultMap = new HashMap<String,Object>();
        String bedroomId = disByStu.getBedroomId();
        String studentNo = disByStu.getStudentNo();
        String oldbedroomId = disByStu.getOldbedroomId();
        System.out.println(oldbedroomId);
        Student student = studentService.getStudentByNo(studentNo);
        //学生原理所在宿舍
        //获取
        Bedroom oldBedroom = null;
        if(!oldbedroomId.equals("")){
         oldBedroom = bedroomService.getBedRoomById(oldbedroomId);}
        //学生现在要分配到的宿舍
        Bedroom bedroom = bedroomService.getBedRoomById(bedroomId);

        //为学生分配新房间的房间号
        student.setBedroomId(bedroomId);
        Integer oldNum = -1;
        Integer num = -1;
        if(oldBedroom!=null) oldNum = Integer.valueOf(oldBedroom.getTotalBed().split("/")[0]);
        System.out.println("split下面的oldNum是"+oldNum);
        if(bedroom!=null)   num= Integer.valueOf(bedroom.getTotalBed().split("/")[0]);
        System.out.println(num);
        if(oldNum!=-1 && oldNum-1 >= 0 ) {oldBedroom.setStatus("N");oldBedroom.setTotalBed( (oldNum-1)+"/" + oldBedroom.getTotalBed().split("/")[1] );}
        if(num!=-1 && num+1 <= Integer.valueOf(bedroom.getTotalBed().split("/")[1])){
            student.setBedroomId(bedroomId);
            student.setBedroomName(bedroom.getBedroomName());
            student.setStatus("Y");
            bedroom.setTotalBed( (num+1)+"/"+bedroom.getTotalBed().split("/")[1]);
            if((num+1)==Integer.valueOf(bedroom.getTotalBed().split("/")[1]) ) bedroom.setStatus("Y");
            else  bedroom.setStatus("N");
        }
        else {resultMap.put("notice","该宿舍已住满");}



        if(oldBedroom!=null){
        bedroomService.update(oldBedroom);
        }
        studentService.update(student);
        bedroomService.update(bedroom);
        DisedStudent disedStudent = new DisedStudent();
        disedStudent.setStudentName(student.getStudentName());
        disedStudent.setBedroomName(bedroom.getBedroomName());
        resultMap.put("student",disedStudent);
        return resultMap;
    }

    /**
     * 按学生情况宿舍
     * @param studentNo
     * @return
     */
    @RequestMapping(value = "/emptyByStu",method = RequestMethod.PUT)
    public Map<String,Object> emptybyStu(String studentNo){
        Map<String,Object> resultMap = new HashMap<String,Object>();
        if(studentNo==null || studentNo.length()<1){
            resultMap.put("notice","还未选择学生");
        }
        Student student = studentService.getStudentByNo(studentNo);
        if(student.getBedroomId()==null || student.getBedroomId()=="" || student.getBedroomId().length()<1){
            resultMap.put("info","该学生还未分配宿舍");
        }
        Bedroom bedroom = bedroomService.getBedRoomById(student.getBedroomId());
        student.setBedroomId("");
        student.setBedroomName("");
        student.setStatus("N");
        bedroom.setStatus("N");
        bedroom.setTotalBed( (Integer.valueOf(bedroom.getTotalBed().split("/")[0])-1)+"/" + bedroom.getTotalBed().split("/")[1] ) ;
        studentService.update(student);
        bedroomService.update(bedroom);
        resultMap.put("student",student.getStudentName());
        return resultMap;
    }

//    public DormResult emptyByStudent(String studnetId){
//
//    }
}
